package com.helen.utils;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.helen.ui.account.RechargeActivity;
import com.helen.ui.newui.BuyRecoveryCarActivity;
import com.helen.ui.order.OrderActivity;
import com.helen.ui.order.PaidOrderActivity;
import com.helen.utils.tiputils.MToast;
import com.weilu.pay.api.RxAliPay;
import com.weilu.pay.api.RxWxPay;
import com.weilu.pay.api.ali.PayResult;
import com.weilu.pay.api.wx.WxPayResult;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * Created by yang on 2018/6/1.
 * 支付宝微信支付
 */

public class RxPayUtils {

    public static void aLiPay(final Activity activity, String signl) {
        new RxAliPay()
                .with(activity, signl)
                .requestPay()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PayResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(PayResult payResult) {
                        if (activity instanceof RechargeActivity) {
                            MToast.makeTextLong(activity, "充值成功").show();
                        }
                        if (activity instanceof PaidOrderActivity) {
                            Intent ti = new Intent(activity, OrderActivity.class);
                            ti.putExtra("currentId", 2);
                            activity.startActivity(ti);
                        }
                        if (activity instanceof BuyRecoveryCarActivity) {
                            MToast.makeTextLong(activity, "购买成功").show();
                            activity.setResult(Activity.RESULT_OK);
                        }
                        activity.finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public static void wechatPay(final Activity activity, String data) {
        String appid = JsonUtil.getJsonValuesString(data, "appid");
        String sign = JsonUtil.getJsonValuesString(data, "sign");
        String partnerid = JsonUtil.getJsonValuesString(data, "partnerid");
        String prepayid = JsonUtil.getJsonValuesString(data, "prepayid");
        String noncestr = JsonUtil.getJsonValuesString(data, "noncestr");
        String timestamp = JsonUtil.getJsonValuesString(data, "timestamp");

        RxWxPay.WXPayBean payBean = new RxWxPay.WXPayBean(appid,
                partnerid, noncestr,
                timestamp, prepayid,
                sign);
        RxWxPay.getInstance()
                .withWxPayBean(payBean)
                .requestPay()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WxPayResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WxPayResult wxPayResult) {
                        if (activity instanceof RechargeActivity) {
                            MToast.makeTextLong(activity, "充值成功").show();
                        }

                        if (activity instanceof PaidOrderActivity) {
                            Intent ti = new Intent(activity, OrderActivity.class);
                            ti.putExtra("currentId", 2);
                            activity.startActivity(ti);
                        }
                        if (activity instanceof BuyRecoveryCarActivity) {
                            MToast.makeTextLong(activity, "购买成功").show();
                            activity.setResult(Activity.RESULT_OK);
                        }
                        activity.finish();

                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
